var QR = require("../../utils/qrcode.js");

Page({

  data: {
    name:"lishi",
    canvasHidden: false,
    imagePath: '',
    placeholder: 'https://www.xxxxx.com/xxxxx/index.html?name=lishi'//默认二维码生成文本
  },

  //动态生成二维码
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.createQrCode('xinxin-34', "mycanvas", 160, 160);
  },


  // 调用插件生成二维码，并生成一张图片的路径
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => { this.canvasToTempImage(); }, 200);
  },
  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function () {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
          // canvasHidden:true
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath;
    console.log(img);
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },

  // 扫码二维码

    downloadCode: function () {
      wx.scanCode({
        success:function(res){
          console.log(res)
        },
        fail:function(){
         
        }
  
      })
     
   }


})